﻿using ManagementApi.Models.Manage.ProjectGroupMember;
using Modules;
using Modules.Corp;
using Modules.Payroll;
using Modules.Project;
using Modules.Project.Entitys;
using PmSoft;
using PmSoft.ApplicationContext;
using static Modules.Payroll.PayrollConstants;

namespace ManagementApi.Models.Payroll
{
    /// <summary>
    /// 工资查询返回实体
    /// </summary>
    public class SalaryInquiryDto
    {
        /// <summary>
        /// 流水号
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProjectName { get; set; }

        /// <summary>
        /// 企业名称
        /// </summary>
        public string CorpName { get; set; }

        /// <summary>
        /// 发放月份
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 人员姓名
        /// </summary>
        public string PersonName { get; set; } = string.Empty;

        /// <summary>
        /// 班组名称
        /// </summary>
        public string GroupName { get; set; } = string.Empty;

        /// <summary>
        /// 工种
        /// </summary>
        public string WorkType { get; set; }

        /// <summary>
        /// 人员身份证号码
        /// </summary>
        public string IdCard { get; set; } = string.Empty;

        /// <summary>
        /// 银行卡号
        /// </summary>
        public string PersonBankNo { get; set; } = string.Empty;

        /// <summary>
        /// 申请金额
        /// </summary>
        public decimal? AmountApplied { get; set; } = 0;

        /// <summary>
        /// 实发金额
        /// </summary>
        public decimal? AmountPaid { get; set; } = 0;

        /// <summary>
        /// 发放时间
        /// </summary>
        public DateTime? IssueTime { get; set; }

        /// <summary>
        /// 状态(1 :已发  0 未发 -1 失败)
        ///</summary>
        public 工资发放状态? Status { get; set; }

        /// <summary>
        /// 状态(1 :已发  0 未发)
        ///</summary>
        public string StatusName { get; set; } = string.Empty;
        public DateTime? CreateTime { get; set; }
    }

    /// <summary>
    /// 扩展
    /// </summary>
    public static partial class SalaryInquiryExceptions
    {
        public static IEnumerable<SalaryInquiryDto> AsDtos(this IEnumerable<WagePayrollDetail> entitys, IApplicationContext applicationContext)
        {
            List<SalaryInquiryDto> dtos = new();
            var projectService = applicationContext.GetService<ProjectService>();
            var payrollService = applicationContext.GetService<WagePayrollService>();
            var corpService = applicationContext.GetService<CorpService>();
            var groupService = applicationContext.GetService<ProjectGroupService>();
            var memberService = applicationContext.GetService<ProjectGroupMemberService>();


            var projects = projectService.GetProjectInfos(entitys.Select(m => m.ProjectId));
            var wagePayrolls = payrollService.GetWagePayrolls(entitys.Select(m => m.PayrollId));
            var groups = groupService.GetProjectGroups(entitys.Select(m => m.GroupId));
            var members = memberService.GetProjectGroupMembers(entitys.Select(m => m.MemberId));
            var corps = corpService.GetCorpInfos(members.Select(m => m.CorpId));

            foreach (var entity in entitys)
            {
                SalaryInquiryDto dto = new();
                var member = members.FirstOrDefault(m => m.Id == entity.MemberId);

                dto.Code = wagePayrolls.FirstOrDefault(m => m.Id == entity.PayrollId)?.Code ?? string.Empty;
                dto.ProjectName = projects.FirstOrDefault(m => m.ProjectId == entity.ProjectId)?.Name ?? string.Empty;
                dto.CorpName = member != null ? corps.FirstOrDefault(m => m.CorpId == member.CorpId)?.Name ?? string.Empty : string.Empty;
                dto.Month = entity.Month;
                dto.PersonName = entity.PersonName;
                dto.GroupName = groups.FirstOrDefault(m => m.GroupId == entity.GroupId)?.Name ?? string.Empty;
                dto.WorkType = member?.Type?.GetDescription() ?? string.Empty;
                dto.IdCard = entity.IdCard;
                dto.PersonBankNo = entity.BankCardNo;
                dto.AmountApplied = entity.AmountApplied;
                dto.AmountPaid = entity.AmountPaid;
                dto.IssueTime = entity.IssueTime;
                dto.Status = entity.Status;
                dto.StatusName = dto.Status?.GetDescription() ?? string.Empty;
                dto.CreateTime = entity.CreateTime;
                dtos.Add(dto);
            }

            return dtos;
        }
    }
}